// pages/fenxiao/cash.js
var app = getApp();
var rootDocment = app.globalData.postUrl;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hiddenLoading: false,
    userInfo: {},
    commission: 0,
    isCash: 0,
    miniCash: 0,
    cash_name:1,//收款人
    cash_bank:'',//开户银行
    bank_account:'',//收款账号
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    //用户授权登录
    app.login(function (userInfo) {
      that.setData({
        userInfo: userInfo
      });
      that.getData();
    });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getData();
    wx.stopPullDownRefresh();
  },

  /**
   * 获取数据
   */
  getData: function () {
    var that = this;
    var paraArr = new Array();
    paraArr['user_id'] = that.data.userInfo.userID;
    var sign = app.signature(paraArr);
    wx.request({
      url: rootDocment + '/api/comm_get/getCashInfo',
      data: {
        user_id: paraArr['user_id'],
        sign: sign
      },
      method: 'GET',
      header: {},
      success: function (res) {
        that.setData({
          commission: res.data.commission,
          isCash: res.data.isCash,
          miniCash: res.data.miniCash,
          bank_account: res.data.bank_account,
          cash_bank: res.data.cash_bank,
          cash_name: res.data.cash_name,
          
          hiddenLoading: true
        });

      }
    })

  },
  /**
   * 申请提现
   */
  formSubmit: function (e) {
    var that = this;
    var m_name = e.detail.value.m_name;
    var m_bank = e.detail.value.m_bank;
    var m_account = e.detail.value.m_account;
    var m_price = e.detail.value.m_price;
    if (m_name == '') {
      wx.showModal({
        title: '提示',
        content: '收款人为空！'
      })
      return false;
    }

    if (m_bank == '') {
      wx.showModal({
        title: '提示',
        content: '开户银行为空！'
      })
      return false;
    }

    if (m_account == '') {
      wx.showModal({
        title: '提示',
        content: '收款账号为空！'
      })
      return false;
    }

    var myreg = /^\d+(\.\d+)?$/;
    if (!myreg.test(m_price)) {
      wx.showModal({
        title: '提示',
        content: '提现金额不正确！'
      })
      return false;
    } else {
      if (m_price <= 0) {
        wx.showModal({
          title: '提示',
          content: '提现金额需大于0！'
        })
        return false;
      }
    }
    let data = {
      m_name: m_name,
      m_bank: m_bank,
      m_account: m_account,
      m_price: m_price,
      user_id: that.data.userInfo.userID
    }
    console.log(data)

    wx.request({
      url: rootDocment + '/api/comm_get/submitCash',
      data: {
        m_name: m_name,
        m_bank: m_bank,
        m_account: m_account,
        m_price: m_price,
        user_id: that.data.userInfo.userID
      },
      method: 'POST',
      header: {},
      success: function (res) {
        console.log(res.data);
        if (res.data.code == 1001) {
          wx.showModal({
            title: '提示',
            content: '提现申请成功，等待管理员审核！',
            showCancel: false,
            success(res) {
              if (res.confirm) {
                wx.navigateBack({
                  delta: 1
                })
              }
            }
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.msg
          })
        }
      }
    })
  }

})